#include "SharedVariables.fxh"

float4x4 World;

float4 lightColor;

sampler depthSampler = sampler_state
{
	Texture=(DepthRT);
	MAGFILTER = POINT;
	MINFILTER = POINT;
	MIPFILTER = POINT;
	AddressU = Clamp;
	AddressV = Clamp;
};

struct DebugVSInput
{
    float4 Position : POSITION0;
};

struct DebugVSOutput
{
    float4 Position : POSITION0;
    float4 ScreenPosition : TEXCOORD0;
    float2 Depth : TEXCOORD1;
};

DebugVSOutput DebugVS(DebugVSInput input)
{
	DebugVSOutput output;
	
	float4 position = mul(input.Position, World);
	output.Position = mul(position, ViewProj);
	
	output.ScreenPosition = output.Position;
    
	output.Depth.x = output.Position.z;
	output.Depth.y = output.Position.w;
	
	return output;
}

float4 DebugPS(DebugVSOutput input) : COLOR0
{	
	input.ScreenPosition.xy /= input.ScreenPosition.w;
	float depth = input.Depth.x / input.Depth.y;
	float2 texCoord = 0.5f * (float2(input.ScreenPosition.x, -input.ScreenPosition.y) + 1.0f);
	texCoord -= halfPixel;
	
	float currentDepth = tex2D(depthSampler, texCoord).x;
	
	float4 color = float4(0,0,0,0);
	
	if (currentDepth >= depth || currentDepth == 1.0f)
	{
		color = lightColor;
	}
	
    return color;
}

technique DrawLightDebug
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 DebugVS();
        PixelShader = compile ps_2_0 DebugPS();
    }
}